package com.liao.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liao.ticket.common.exception.ErrorCode;
import com.liao.ticket.dao.mapper.PassengerMapper;
import com.liao.ticket.dao.pojo.Passenger;
import com.liao.ticket.service.PassengerService;
import com.liao.ticket.util.UserThreadLocal;
import com.liao.ticket.vo.PassengerVo;
import com.liao.ticket.vo.Result;
import com.liao.ticket.vo.params.PassengerParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Service
@Transactional
public class PassengerServiceImpl implements PassengerService {

    @Autowired
    private PassengerMapper passengerMapper;

    @Override
    public Result selectAllByUid() {
        List<Passenger> passengerList = passengerMapper.selectList(wrapper(null));
        if (passengerList == null) {
            return Result.success(new ArrayList<>());
        }
        return Result.success(copy(passengerList));
    }

    @Override
    public Result selectByIdAndUid(Long id) {
        Passenger passenger = passengerMapper.selectOne(wrapper(id));
        if (passenger == null) {
            return Result.fail(ErrorCode.USER_PASSENGER_NO_EXIT);
        }
        return Result.success(copy(passenger));
    }

    @Override
    public Result selectByPhoneAndUid(String phone) {
        if (StringUtils.isBlank(phone)) {
            return Result.fail(ErrorCode.USER_PASSENGER_QUERY_PARAMS_FAIL);
        }
        LambdaQueryWrapper<Passenger> wrapper = wrapper(null);
        wrapper.eq(Passenger::getPhone, phone);
        Passenger passenger = passengerMapper.selectOne(wrapper);
        if (passenger == null) {
            return Result.fail(ErrorCode.USER_PASSENGER_NO_EXIT);
        }
        return Result.success(copy(passenger));
    }

    @Override
    public Result addOne(PassengerParams passengerParams) {
        String name = passengerParams.getName();
        String phone = passengerParams.getPhone();
        String idcard = passengerParams.getIdcard();
        if (StringUtils.isBlank(name) || StringUtils.isBlank(phone) || StringUtils.isBlank(idcard)) {
            return Result.fail(ErrorCode.USER_PASSENGER_ADD_PARAMS_FAIL);
        }
        List<Passenger> passengerList = selectPassengerList(phone, name, idcard);
        System.out.println(passengerList.size());
        if (passengerList != null && passengerList.size() > 0) {
            return Result.fail(ErrorCode.USER_PASSENGER_ADD_EXIT);
        }
        Passenger passenger = new Passenger();
        passenger.setUserId(UserThreadLocal.get().getId());
        passenger.setName(name);
        passenger.setIdcard(idcard);
        passenger.setPhone(phone);
        return Result.success(passengerMapper.insert(passenger));
    }

    @Override
    public Result updateOneById(PassengerParams passengerParams) {
        Long id = passengerParams.getId();
        String phone = passengerParams.getPhone();
        if(StringUtils.isBlank(phone) || id == null){
            return Result.fail(ErrorCode.USER_PASSENGER_UPDATE_PARAMS_FAIL);
        }
        LambdaQueryWrapper<Passenger> wrapper = wrapper(id);
        Passenger passenger = new Passenger();
        passenger.setPhone(phone);
        return Result.success(passengerMapper.update(passenger, wrapper));
    }

    @Override
    public Result deleteAllByUid() {
        return Result.success(passengerMapper.delete(wrapper(null)));
    }

    @Override
    public Result deleteOneById(Long id) {
        return Result.success(passengerMapper.delete(wrapper(id)));
    }

    @Override
    public PassengerVo selectOneById(Long passengerId) {
        Passenger passenger = passengerMapper.selectById(passengerId);
        if(passenger == null){
            return null;
        }
        return copy(passenger);
    }

    @Override
    public Passenger findOneByIdAndUid(Long passengerId) {
        return passengerMapper.selectOne(wrapper(passengerId));
    }

    private List<Passenger> selectPassengerList(String phone, String name, String idcard) {
        LambdaQueryWrapper<Passenger> wrapper = wrapper(null);
        wrapper.and(qw -> qw.eq(Passenger::getPhone, phone).or().eq(Passenger::getName, name).or().eq(Passenger::getIdcard, idcard));
        List<Passenger> passengerList = passengerMapper.selectList(wrapper);
        return passengerList;
    }

    private LambdaQueryWrapper<Passenger> wrapper(Long id) {
        LambdaQueryWrapper<Passenger> wrapper = new LambdaQueryWrapper<>();
        Long uid = UserThreadLocal.get().getId();
        if (id != null) {
            wrapper.eq(Passenger::getId, id);
        }
        return wrapper.eq(Passenger::getUserId, uid);
    }

    private PassengerVo copy(Passenger passenger) {
        PassengerVo passengerVo = new PassengerVo();
        BeanUtils.copyProperties(passenger, passengerVo);
        passengerVo.setIdcard(passenger.getIdcard());
        return passengerVo;
    }

    private List<PassengerVo> copy(List<Passenger> passengerList) {
        List<PassengerVo> passengerVoList = new ArrayList<>();
        for (Passenger passenger : passengerList) {
            passengerVoList.add(copy(passenger));
        }
        return passengerVoList;
    }
}
